package com.mall.service.impl;

import com.github.pagehelper.PageInfo;
import com.mall.bean.Collection;
import com.mall.bean.CollectionExample;
import com.mall.dao.CollectionMapper;
import com.mall.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.awt.*;
import java.util.Date;
import java.util.List;

/**
 * @author lwd1513
 * @date 2022-04-25 14 43
 * discription
 */
@Service
public class CollectionServiceImpl implements CollectionService {
    @Autowired
    private CollectionMapper collectionMapper;

    @Override
    public int insert(Collection collection) {
        return 0;
    }

    @Override
    public List<Collection> queryData(Collection collection) {
        return null;
    }

    @Override
    public int update(Collection collection) {
        return 0;
    }

    @Override
    public int deleteById(String id) {
        return 0;
    }

    @Override
    public Collection queryDataById(String id) {
        return null;
    }

    @Override
    public PageInfo<Collection> queryDataByPage(int currentPage, int pageSize, Collection collection) {
        return null;
    }

    @Override
    public List<Collection> getCollectionByMemberId(String memberId) {
        CollectionExample example = new CollectionExample();
        CollectionExample.Criteria criteria = example.createCriteria();
        criteria.andMember_idEqualTo(Integer.parseInt(memberId));
        criteria.andFlagEqualTo(1);
        List<Collection> collections = collectionMapper.selectByExample(example);
        return collections;
    }

    @Override
    public Collection getCollectionByMemberIdAndGoodsId(String goodsId, String memberId) {
        CollectionExample collectionExample = new CollectionExample();
        CollectionExample.Criteria criteria = collectionExample.createCriteria();
        criteria.andFlagEqualTo(1);
        criteria.andGoods_idEqualTo(Integer.parseInt(goodsId));
        criteria.andMember_idEqualTo(Integer.parseInt(memberId));

        List<Collection> collections = collectionMapper.selectByExample(collectionExample);
        if (collections.size() > 0) {
            return collections.get(0);
        }
        return null;
    }

    @Override
    public int collectionGoodsById(String goodsId, String memberId) {
        Collection collection = new Collection();
        collection.setMember_id(Integer.parseInt(memberId));
        collection.setGoods_id(Integer.parseInt(goodsId));
        collection.setCollection_id(new Date().getTime() + "");
        collection.setCollection_time(new Date());
        collection.setFlag(1);

        int res = collectionMapper.insertSelective(collection);

        return res;
    }

    @Override
    public int removeFavoriteById(String itemId) {
        Collection collection = new Collection();
        collection.setFlag(0);
        collection.setCollection_id(itemId);
        int res = collectionMapper.updateByPrimaryKeySelective(collection);
        return res;
    }

    @Override
    public int removeFavoritemArrayById(String[] idArray) {
        int res = collectionMapper.removeFavoritemArrayById(idArray);
        return res;
    }
}
